﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    /// <summary>
    /// RESPONSABILITY: generate random IDs
    /// </summary>
    public class IDGenerator
    {
        static private Dictionary<Type, List<int>> ids { get; set; }
        List<int> listOfIds;

        static readonly IDGenerator instance = new IDGenerator();
        public static  IDGenerator Instance
        {
            get {
                return instance;
            }
        }
        

        public IDGenerator()
        {
            ids  =new Dictionary<Type,List<int>>() ;
        }

        public int RandomIDGenerator(Type type)
        {
            if (!ids.TryGetValue(type, out listOfIds))
            {

                listOfIds = new List<int>();
                ids[type]  = listOfIds;
            }
          
            Random randomID = new Random();
            int Id;

            do
            {
                Id = randomID.Next();      
            }
            while ((CheckIdExistance(Id)));
            
            listOfIds.Add(Id);
            return Id;            
        }

        private bool CheckIdExistance(int Id)
        {
            foreach (var id in listOfIds)
            {
                if (id == Id)
                {
                    return true; ;
                }
            }
            return false;
        }
        
    }
}
